package com.sunxiansheng.user.controller;

import com.alibaba.fastjson.JSON;
import com.sunxiansheng.response.Result;
import com.sunxiansheng.user.convert.SysUserConvert;
import com.sunxiansheng.user.entity.dto.SysUserDto;
import com.sunxiansheng.user.entity.page.PageResult;
import com.sunxiansheng.user.entity.req.SysUserReq;
import com.sunxiansheng.user.entity.vo.SysUserVo;
import com.sunxiansheng.user.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (SysUser)控制层
 *
 * @author sun
 * @since 2024-07-18 16:59:16
 */
@RestController
@RequestMapping("/sys/user")
@Slf4j
@Api(tags = "用户controller") // 使用swagger标记
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 分页查询数据
     *
     * @param req 筛选条件
     * @return 查询结果
     */
    @GetMapping("/queryPage")
    @ApiOperation(value = "分页查询", notes = "分页查询的notes")
    public Result<PageResult<SysUserVo>> queryByPage(@RequestBody SysUserReq req) {
        try {
            // 打印日志
            if (log.isInfoEnabled()) {
                log.info("分页查询数据入参{}", JSON.toJSONString(req));
            }

            // ============================== Preconditions 参数校验 ==============================

            // ============================== Preconditions 参数校验 ==============================

            // 将req转换为dto（如果req的字段符合service层的规范，不转也可以）
            SysUserDto sysUserDto = SysUserConvert.INSTANCE.convertReqToDto(req);
            // 调用service层
            PageResult<SysUserVo> sysUserVoPageResult = this.sysUserService.queryByPage(sysUserDto);
            return Result.ok(sysUserVoPageResult);
        } catch (Exception e) {
            // 打印error日志
            log.error("分页查询数据！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
}
